//
//  HomeTableViewController.swift
//  weibo
//
//  Created by 谭仁川 on 15/11/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD


private let HomeCellId = "HomeCellId"
class  HomeTableViewController: BaseTableViewController {
    
    private lazy var statuses = [Status]()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !userLogin {
        
        visitorLoginView?.setWithInfo("关注一些人，回这里看看有什么惊喜", imageName: nil)
            
            return 
        
        }
        
        prepareTableView()
        loadData()
    }
    
    private func prepareTableView(){
        //注册cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: HomeCellId)
    
        //tableView.rowHeight = 200
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None
    }
    
    private func loadData() {
        StatusListViewModel.loadData { (statuses) -> () in
            guard let list = statuses else {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            self.statuses = list
            
            self.tableView.reloadData()

        }
        
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
        return statuses.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCell
        cell.status = statuses[indexPath.row]
        
        return cell
    }

}
